//
//  MainViewController.m
//  CoolFactsClass
//
//  Created by rimi on 16/7/12.
//  Copyright © 2016年 wangxinyao. All rights reserved.
//

#import "MainViewController.h"
#import "CollectionViewController.h"
#import "MessageViewController.h"
#import "PersonalViewController.h"
@interface MainViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
{
    UIImagePickerController *getImage;
}
@property (nonatomic, assign) BOOL isShowRight;
// 抽屉效果 约束
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *CuriousnessLeadingConstraint;
// 抽屉效果 手势
@property (nonatomic, strong) UIPanGestureRecognizer * panGuesturer;
@property (nonatomic, strong) UIButton * collectionBtn;
@property (nonatomic, strong) UIButton * messagesBtn;
@property (nonatomic, strong) UIButton * informationBtn;
@property (nonatomic, strong) UIButton * loginOutBtn;

@property (nonatomic, strong) UIImageView * bgImageView;
@property (strong, nonatomic) UIAlertController * alert;
// 个人信息视图


// init
- (void)initializeDataSource;
- (void)initializeUserInterface;
// Action
- (void)panGesturerHandle:(UIPanGestureRecognizer *)panGesturer;
@end

@implementation MainViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeDataSource];
    [self initializeUserInterface];
}

#pragma mark - 初始化界面、数据
- (void)initializeDataSource{
    _isShowRight = NO;
}

- (void)initializeUserInterface{
    //毛玻璃效果
    self.bgImageView = [UIImageView new];
    self.bgImageView.bounds = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds));
    self.bgImageView.userInteractionEnabled = YES;
    self.bgImageView.center = self.view.center;
    self.bgImageView.image = [UIImage imageNamed:@"background_main.jpg"];
    [self.view addSubview:self.bgImageView];
    UIBlurEffect * blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView * effectview = [[UIVisualEffectView alloc]initWithEffect:blur];
    effectview.frame = CGRectMake(0, 0, self.bgImageView.frame.size.width, self.bgImageView.frame.size.height);
    [effectview.contentView setUserInteractionEnabled:YES];
    [self.bgImageView addSubview:effectview];
    getImage = [[UIImagePickerController alloc]init];
    //头像
    UIImageView * showImage = [UIImageView new];
    showImage.backgroundColor = [UIColor whiteColor];
    showImage.image = [UIImage imageNamed:@"头像.jpg"];
    showImage.layer.masksToBounds = YES;
    showImage.layer.cornerRadius = CGRectGetWidth(self.view.bounds)/6;
    showImage.bounds = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds)/3, CGRectGetWidth(self.view.bounds)/3);
    showImage.center = CGPointMake(CGRectGetWidth(self.view.bounds) / 1.5, CGRectGetHeight(self.view.bounds) / 4);
    [showImage setUserInteractionEnabled:YES];
    [showImage addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(loginOrChange)]];
    [effectview.contentView addSubview:showImage];
    
    //用户名 + 等级积分
    UILabel * nameStr = [UILabel new];
    nameStr.text = @"王大锤     清真";
    nameStr.font = [UIFont systemFontOfSize:22];
    nameStr.textAlignment = NSTextAlignmentCenter;
    nameStr.bounds = CGRectMake(0, 0,CGRectGetWidth(self.view.bounds) / 2, CGRectGetHeight(self.view.bounds)/24);
    nameStr.center = CGPointMake(CGRectGetWidth(self.view.bounds) / 1.5, CGRectGetHeight(self.view.bounds) / 2.5);
    [effectview.contentView  addSubview:nameStr];
    //收藏夹按钮
    self.collectionBtn = [UIButton new];
    self.collectionBtn.backgroundColor = [[UIColor colorWithRed:0.705 green:0.865 blue:1.000 alpha:1.000]colorWithAlphaComponent:0.5f];
    self.collectionBtn.layer.cornerRadius = 5;
    [self.collectionBtn setTitle:@" 收 藏 " forState:UIControlStateNormal];
    self.collectionBtn.layer.shadowOffset = CGSizeMake(5, 5);
    self.collectionBtn.layer.shadowColor = [UIColor blackColor].CGColor;
    self.collectionBtn.layer.shadowOpacity = 0.5;
    [self.collectionBtn addTarget:self action:@selector(BtnClickDown:) forControlEvents:UIControlEventTouchDown];
    [self.collectionBtn addTarget:self action:@selector(collectionBtnClick:)forControlEvents:UIControlEventTouchUpInside];
    self.collectionBtn.bounds = CGRectMake(0, 0,CGRectGetWidth(self.view.bounds) / 2.2, CGRectGetHeight(self.view.bounds)/24);
    self.collectionBtn.center = CGPointMake(CGRectGetWidth(self.view.bounds) / 1.5, CGRectGetHeight(self.view.bounds) / 2.2);
    [effectview.contentView addSubview:self.collectionBtn];
    //消息按钮
    self.messagesBtn = [UIButton new];
    self.messagesBtn.backgroundColor = [[UIColor colorWithRed:0.702 green:0.786 blue:1.000 alpha:1.000]colorWithAlphaComponent:0.5f];
    self.messagesBtn.layer.cornerRadius = 5;
    [self.messagesBtn setTitle:@" 消 息 " forState:UIControlStateNormal];
    self.messagesBtn.layer.shadowOffset = CGSizeMake(5, 5);
    self.messagesBtn.layer.shadowColor = [UIColor blackColor].CGColor;
    self.messagesBtn.layer.shadowOpacity = 0.5;
    [self.messagesBtn addTarget:self action:@selector(BtnClickDown:) forControlEvents:UIControlEventTouchDown];
    [self.messagesBtn addTarget:self action:@selector(messagesBtnClick:)forControlEvents:UIControlEventTouchUpInside];
    self.messagesBtn.bounds = CGRectMake(0, 0,CGRectGetWidth(self.view.bounds) / 2.2, CGRectGetHeight(self.view.bounds)/24);
    self.messagesBtn.center = CGPointMake(CGRectGetWidth(self.view.bounds) / 1.5, CGRectGetHeight(self.view.bounds) / 2.2 + 40);
    [effectview.contentView addSubview:self.messagesBtn];
    //个人信息按钮
    self.informationBtn = [UIButton new];
    self.informationBtn.backgroundColor = [[UIColor colorWithRed:0.812 green:0.692 blue:1.000 alpha:1.000]colorWithAlphaComponent:0.5f];
    self.informationBtn.layer.cornerRadius = 5;
    [self.informationBtn setTitle:@" 个 人 信 息 " forState:UIControlStateNormal];
    self.informationBtn.layer.shadowOffset = CGSizeMake(5, 5);
    self.informationBtn.layer.shadowColor = [UIColor blackColor].CGColor;
    self.informationBtn.layer.shadowOpacity = 0.5;
    [self.informationBtn addTarget:self action:@selector(BtnClickDown:) forControlEvents:UIControlEventTouchDown];
    [self.informationBtn addTarget:self action:@selector(informationBtnClick:)forControlEvents:UIControlEventTouchUpInside];
    self.informationBtn.bounds = CGRectMake(0, 0,CGRectGetWidth(self.view.bounds) / 2.2, CGRectGetHeight(self.view.bounds)/24);
    self.informationBtn.center = CGPointMake(CGRectGetWidth(self.view.bounds) / 1.5, CGRectGetHeight(self.view.bounds) / 2.2 + 80);
    [effectview.contentView addSubview:self.informationBtn];
    //注销按钮
    self.loginOutBtn = [UIButton new];
    self.loginOutBtn.backgroundColor = [[UIColor colorWithRed:0.990 green:0.767 blue:1.000 alpha:1.000]colorWithAlphaComponent:0.5f];
    self.loginOutBtn.layer.cornerRadius = 5;
    [self.loginOutBtn setTitle:@" 注 销 " forState:UIControlStateNormal];
    self.loginOutBtn.layer.shadowOffset = CGSizeMake(5, 5);
    self.loginOutBtn.layer.shadowColor = [UIColor blackColor].CGColor;
    self.loginOutBtn.layer.shadowOpacity = 0.5;
    [self.loginOutBtn addTarget:self action:@selector(BtnClickDown:) forControlEvents:UIControlEventTouchDown];
    [self.loginOutBtn addTarget:self action:@selector(loginOutBtnClick:)forControlEvents:UIControlEventTouchUpInside];
    self.loginOutBtn.bounds = CGRectMake(0, 0,CGRectGetWidth(self.view.bounds) / 2.2, CGRectGetHeight(self.view.bounds)/24);
    self.loginOutBtn.center = CGPointMake(CGRectGetWidth(self.view.bounds) / 1.5, CGRectGetHeight(self.view.bounds) / 2.2 + 120);
    [effectview.contentView  addSubview:self.loginOutBtn];
    //背景图片变化按钮
    UIButton * bgChangeBtn = [UIButton new];
    bgChangeBtn.backgroundColor = [UIColor clearColor];
    bgChangeBtn.layer.cornerRadius = 5;
    [bgChangeBtn setBackgroundImage:[UIImage imageNamed:@"图片选取.jpg"] forState:UIControlStateNormal];
     [bgChangeBtn addTarget:self action:@selector(bgChangeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    bgChangeBtn.bounds = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds) / 14, CGRectGetWidth(self.view.bounds) / 14);
    bgChangeBtn.center = CGPointMake(CGRectGetWidth(self.view.bounds) / 3, CGRectGetHeight(self.view.bounds) / 18);
    [effectview.contentView  addSubview:bgChangeBtn];
    
    //copyright
    UILabel * copyrightLabel = [UILabel new];
    copyrightLabel.text = @"copyright by ギリギリeye";
    copyrightLabel.textAlignment = NSTextAlignmentCenter;
    copyrightLabel.textColor = [UIColor redColor];
    copyrightLabel.layer.shadowOffset = CGSizeMake(0, 5);
    copyrightLabel.layer.shadowColor = [UIColor blackColor].CGColor;
    copyrightLabel.layer.shadowOpacity = 0.7;
    copyrightLabel.bounds = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds) / 2, CGRectGetHeight(self.view.bounds)/28);
    copyrightLabel.center = CGPointMake(CGRectGetWidth(self.view.bounds) / 1.5, CGRectGetHeight(self.view.bounds) / 1.02);
    copyrightLabel.backgroundColor = [[UIColor grayColor]colorWithAlphaComponent:0.2f];
    [effectview.contentView  addSubview:copyrightLabel];
}
#pragma mark - BtnAction
- (void)loginOrChange{
    NSLog(@"点击头像");
}
- (void)BtnClickDown:(UIButton *)sender{
    sender.layer.shadowOpacity = 0;
}
- (void)collectionBtnClick:(UIButton *)sender{
    sender.selected = !sender.selected;
    sender.layer.shadowOpacity = 0.5;
    
    CollectionViewController * vc = [self.storyboard instantiateViewControllerWithIdentifier:@"CollectionView"];
    [self presentViewController:vc animated:YES completion:nil];
//    [self.navigationController pushViewController:vc animated:YES];
    
    NSLog(@"收藏");
}
- (void)messagesBtnClick:(UIButton *)sender{
    sender.selected = !sender.selected;
    sender.layer.shadowOpacity = 0.5;
    MessageViewController * vc = [self.storyboard instantiateViewControllerWithIdentifier:@"MessageView"];
    [self presentViewController:vc animated:YES completion:nil];
    NSLog(@"消息");
}
- (void)informationBtnClick:(UIButton *)sender{
    sender.selected = !sender.selected;
    sender.layer.shadowOpacity = 0.5;
    PersonalViewController * vc = [self.storyboard instantiateViewControllerWithIdentifier:@"PersonalView"];
    [self presentViewController:vc animated:YES completion:nil];
    NSLog(@"个人信息");
}
- (void)loginOutBtnClick:(UIButton *)sender{
    sender.selected = !sender.selected;
    sender.layer.shadowOpacity = 0.5;
    NSLog(@"注销");
}
- (void)bgChangeBtnClick:(UIButton *)sender{
    NSLog(@"改变背景图");
    _alert = self.alert;
    [self presentViewController:_alert animated:YES completion:^{
        
    }];
}
#pragma mark --UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info  {
    self.bgImageView.image = info[UIImagePickerControllerEditedImage];
    [self dismissViewControllerAnimated:YES completion:nil];
    if (getImage.sourceType == UIImagePickerControllerSourceTypePhotoLibrary) {
        
    }
}
#pragma mark - Action
// 抽屉
- (void)showRightViewOrHidden{
    if (self.isShowRight) {
        // 如果右侧抽屉展开，则隐藏
        self.CuriousnessLeadingConstraint.constant = 0;
    }else{
        // 如果右侧抽屉隐藏，则展开
        self.CuriousnessLeadingConstraint.constant = CGRectGetWidth([UIScreen mainScreen].bounds) * 0.75;
    }
    [UIView animateKeyframesWithDuration:0.3 delay:0 options:UIViewKeyframeAnimationOptionCalculationModeLinear animations:^{
        [self.view layoutIfNeeded];
    } completion:^(BOOL finished) {
        self.isShowRight = !self.isShowRight;
        // 手势
        if (self.isShowRight) {
            // 当前抽屉展开 添加手势
            self.panGuesturer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesturerHandle:)];
            [self.view addGestureRecognizer:self.panGuesturer];
        }else{
            // 当前抽屉关闭 移除手势
            [self.view removeGestureRecognizer:self.panGuesturer];
        }
    }];
}

- (void)panGesturerHandle:(UIPanGestureRecognizer *)panGesturer{
    // 手势移动的距离
    CGPoint currentPoint= [panGesturer translationInView:self.view];
    CGFloat maxX = CGRectGetWidth([UIScreen mainScreen].bounds) * 0.75;
    CGFloat desX = self.CuriousnessLeadingConstraint.constant - currentPoint.x;
    if (panGesturer.state == UIGestureRecognizerStateEnded) {
        // 手势结束 调整抽屉的位置
        if(desX < maxX / 2){
            // 在抽屉区域右侧 则关闭抽屉
            desX = 0;
        }else{
            // 在抽屉区域左侧 则展开抽屉
            desX = maxX;
        }
        self.CuriousnessLeadingConstraint.constant = desX;
        [UIView animateKeyframesWithDuration:0.3 delay:0 options:UIViewKeyframeAnimationOptionCalculationModeLinear animations:^{
            [self.view layoutIfNeeded];
        } completion:^(BOOL finished) {
            if (desX == 0) {
                // 当前抽屉从打开到关闭
                self.isShowRight = NO;
                [self.view removeGestureRecognizer:self.panGuesturer];
            }
        }];
    }else{
        // 在手势拖动过程中
        if(desX < 0){
            desX = 0;
        }else if(desX > maxX){
            desX = maxX;
        }
        self.CuriousnessLeadingConstraint.constant = desX;
        [self.view layoutIfNeeded];
    }
    [self.panGuesturer setTranslation:CGPointZero inView:self.view];
}
#pragma mark -- Getters
- (UIAlertController *)alert {
    if (!_alert) {
        _alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"请选择图片来源" preferredStyle:UIAlertControllerStyleActionSheet];
        [_alert addAction:[UIAlertAction actionWithTitle:@"图库" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            //获取图库
            NSArray * arr = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
            //判断是否支持需要设置的soureceType类型
            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
                getImage.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                getImage.mediaTypes = @[arr[0]];
                //设置是否编辑
                getImage.allowsEditing = YES;
                getImage.delegate = self;
                [self presentViewController:getImage animated:YES completion:nil];
            }
        }]];
        [_alert addAction:[UIAlertAction actionWithTitle:@"照相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            //获取相机
            //            NSArray * arr = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
            UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"系统模拟器暂不能提供照相功能" preferredStyle:UIAlertControllerStyleAlert];
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]];
            [self presentViewController:alert animated:YES completion:nil];
        }]];
        [_alert addAction:[UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            //获取相簿
            NSArray * arr = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeSavedPhotosAlbum];
            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) {
                getImage.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
                getImage.mediaTypes = @[arr[0]];
                //设置是否编辑
                getImage.allowsEditing = YES;
                getImage.delegate = self;
                [self presentViewController:getImage animated:YES completion:nil];
            }
        }]];
        [_alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    }
    return _alert;
}

@end
